let Main = {
    data() {
        return {
            // userId: '2781934384054272',
            userId: '',
            phoneNumber: '',
            sex: '',
            userPoints: '',
            MemberRegistration: true,
            sendSubDisabled: true,
            vip: {
                code: '',
                phoneNumber: ''
            },
            integral: 0,
            isSending: false,
            countdown: 0,
            userOrders: [],
        }
    },
    created() {
        // this.queryOrders(this.userId, 1);
    },
    methods: {
        // 关闭登录弹窗
        cancelMember() {
            this.MemberRegistration = false;
            this.sendSubDisabled = true;
            this.vip = {
                code: '',
                phoneNumber: ''
            }
            this.integral = 0;
        },
        // 发送验证码
        sendCode() {
            if (this.countdown > 0 || this.isSending) {
                this.$message.error("请勿连续点击");
                return; // 防止重复点击发送
            }
            if (this.vip.phoneNumber === '' || this.vip.phoneNumber.toString().length !== 11) {
                this.$message.error("电话号码格式错误，请输入正确的电话号码");
                return;
            }
            let _this = this;
            axios.get("/apm/message/sendLoginCode?phoneNumber=" + _this.vip.phoneNumber)
                .then(function (resp) {
                    if (resp.data.isSuccess === 200) {
                        _this.isSending = true;
                        _this.startCountdown();
                        _this.sendSubDisabled = false;
                    } else {
                        _this.$message.error("发送失败，请重新发送");
                    }
                }).catch(function (error) {
                console.log(error);
                _this.$message.error("发送失败，请重新发送");
            });

        },
        // 倒计时
        startCountdown() {
            this.isSending = false;
            this.countdown = 60;
            const timer = setInterval(() => {
                this.countdown--;
                if (this.countdown <= 0) {
                    clearInterval(timer);
                }
            }, 1000);
        },
        // 提交登录
        vipForm() {
            let vip = this.vip;
            if (vip["code"] === '' || vip["phoneNumber"] === '') {
                this.$message.error("验证码和电话号码不为空");
                return;
            }
            let _this = this;
            axios.get('/apm/message/verifyCode?phoneNumber=' + vip["phoneNumber"] + '&code=' + vip["code"])
                .then(function (resp) {
                    if (resp.data.isSuccess === 200) {
                        _this.$message.success("登录成功!!!");
                        _this.phoneNumber = vip["phoneNumber"];
                        _this.isVip = true;
                        _this.userId = resp.data.userId;
                        _this.queryOrders(_this.userId, 0);
                        _this.MemberRegistration = false;
                    } else {
                        _this.$message.error("验证码错误，请重试");
                    }
                })
                .catch(function (e) {
                    _this.$message.error(e);
                })
        },
        // 登录成功之后查询订单
        queryOrders(userId, param) {
            if (param === 1 && this.userId === '') {
                this.MemberRegistration = true;
                return;
            }
            console.log(userId);
            let _this = this;
            axios.post('/apm/queryOrder/queryUserOrder?' +
                "userId=" + userId +
                "&time=2023-12-31 12:12:12")
                .then(function (resp) {
                    if (200 === resp.data.isSuccess) {
                        _this.userOrders = resp.data.data;
                        _this.sex = resp.data.sex;
                        _this.userPoints = resp.data.userPoints;
                    } else {
                        console.log(resp.data);
                        _this.$message.error("订单查询失败!!!!");
                    }
                }).catch(function (e) {
                console.log(e);
                _this.$message.error("订单查询失败!!!!");
            })
        },
        // 选择性别
        handleSex(command) {
            console.log(command);
            let _this = this;
            axios.get('/apm/user/updateSex?userId=' + _this.userId + '&sex=' + command)
                .then(function (resp) {
                    if (200 === resp.data.isSuccess) {
                        _this.sex = command;
                        _this.$message.success("修改成功，已将性别修改为" + (_this.sex === '0' ? '女' : '男'));
                    } else {
                        _this.$message.error("修改失败，请重试");
                    }
                }).catch(function (e) {
                console.log(e);
                _this.$message.error("修改失败，请重试");
            })
        }
    },
}
let Ctor = Vue.extend(Main)
new Ctor().$mount('#app')